package com.ggh.jinjilive.dialog.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemCreateRoomBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.create.ChooseRoom;

import java.util.List;

public class CreateRoomAdapter extends RecyclerView.Adapter<CreateRoomAdapter.ItemViewHolder> {
    private List<ChooseRoom> chooseRoomList;

    private ChooseRoom chooseRoom;

    public void setAdd(List<ChooseRoom> chooseRoomList,ChooseRoom chooseRoom) {
        this.chooseRoomList = chooseRoomList;
        this.chooseRoom = chooseRoom;
        notifyDataSetChanged();
    }

    private AddItemOnClickListener addItemOnClickListener;

    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener) {
        this.addItemOnClickListener = addItemOnClickListener;
    }

    @NonNull
    @Override
    public CreateRoomAdapter.ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemCreateRoomBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_create_room, parent, false);
        return new ItemViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull CreateRoomAdapter.ItemViewHolder holder, int position) {
        ChooseRoom chooseRoom = chooseRoomList.get(position);
        if(null != chooseRoom){
            if(null != this.chooseRoom){
                if(this.chooseRoom.getID() == chooseRoom.getID()){
                    holder.binding.llChooseRoom.setBackgroundResource(R.drawable.bg_1_5_7ebfff);
                    holder.binding.imgRoomName.setTextColor(Color.parseColor("#BADBFC"));
                }else {
                    holder.binding.llChooseRoom.setBackgroundResource(R.drawable.bg_1_5_74);
                    holder.binding.imgRoomName.setTextColor(Color.parseColor("#ffffff"));
                }
            }else {
                holder.binding.llChooseRoom.setBackgroundResource(R.drawable.bg_1_5_74);
                holder.binding.imgRoomName.setTextColor(Color.parseColor("#ffffff"));
            }
            holder.binding.imgRoomPicture.setImageResource(chooseRoom.getRoomPicture());
            holder.binding.imgRoomName.setText(chooseRoom.getRoomName());
            holder.binding.llChooseRoom.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(null != addItemOnClickListener){
                        addItemOnClickListener.onSkipClicked(position,chooseRoom);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return null == chooseRoomList ? 0 : chooseRoomList.size();
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        ItemCreateRoomBinding binding;

        public ItemViewHolder(@NonNull ItemCreateRoomBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}